<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <!-- 左边标题 -->
      <h1>欢迎来到叩丁狼积分商城</h1>
      <!-- 右边用户内容 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="23px" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li
          class="login-btn cart"
          v-if="isLogined"
          @click="$router.push('/user/cart')"
        >
          <img src="../assets/img/cart.png" alt="" />
          购物车
          <p>{{ cartTotal }}</p>
        </li>
        <li class="login-btn" @click="changeIsShowModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { wxLoginAPI } from "@/request/Api";
export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowModal: "showModal/changeIsShowModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncUpdateUserInfo: "userInfo/asyncUpdateUserInfo",
    }),
  },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         let token = localStorage.getItem("x-auth-token");
  //         this.changeIsLogined(Boolean(token));
  //       }
  //     },
  //   },
  // },
  async created() {
    let code = this.$route.query.code;
    // console.log(code);
    // 有code去换token
    if (code) {
      let res = await wxLoginAPI({
        code,
      });

      console.log("code", res);
      if (res.data.code == 400) {
        // 提示二维码过期,重新扫码
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "二维码过期,请重新扫码",
          type: "warning",
        });
        // 打开模态框
        this.changeIsShowModal(true);
      } else if (res.data.code == 407) {
        //提示绑定
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请绑定手机号,重新登陆",
          type: "warning",
        });
        // 打开模态框
        this.changeIsShowModal(true);
        // 保存uuid
        localStorage.setItem("uuid", res.data.uuid);
      } else if (res.data.code == 0) {
        // 提示扫码登录成功
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "扫码登录成功",
          type: "success",
        });
        // 存token
        localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
        // 改变登录为购物车
        this.changeIsLogined(true);
        this.$router.push(this.$route.path);
        // 微信登录渲染用户信息
        this.asyncUpdateUserInfo();
      }
    } else {
      // 拿不到code
      // 根据用户状态判断购物车/登录按钮
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLogined(Boolean(token));
      // 更新用户信息会重载topBar,手动删除token初始化用户信息
      if (!token) {
        this.initUserInfo();
      } else {
        this.asyncUpdateUserInfo();
      }
    }
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-family: SourceHanSansSC;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        margin-left: 20px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background: #0a328e;
          text-align: center;
        }
        &.cart {
          img {
            width: 22px;
            height: 22px;
          }
          p {
            width: 20px;
            height: 20px;
            line-height: 20px;
            border-radius: 50%;
            color: #fff;
            background-color: red;
          }
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>